﻿using System;
using System.Collections.Generic;
using System.IO.Compression;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace IM.Easy.Core.Utils
{
    /// <summary>
    /// 文件操作工具类
    /// </summary>
    public class FileUtils
    {
        /// <summary>
        /// 生成并打包指定路径下的多个文件为 ZIP 文件，并返回文件流。
        /// </summary>
        /// <param name="filePaths">文件路径集合</param>
        /// <returns>ZIP 文件流</returns>
        public static MemoryStream CreateZipFile(IEnumerable<string> filePaths)
        {
            var memoryStream = new MemoryStream();
            using (var zipArchive = new ZipArchive(memoryStream, ZipArchiveMode.Create, leaveOpen: true))
            {
                foreach (var filePath in filePaths)
                {
                    var fileName = Path.GetFileName(filePath);
                    var zipEntry = zipArchive.CreateEntry(fileName, CompressionLevel.Optimal);

                    using var entryStream = zipEntry.Open();
                    using var fileStream = new FileStream(filePath, FileMode.Open, FileAccess.Read);
                    fileStream.CopyTo(entryStream);
                }
            }

            memoryStream.Position = 0;
            return memoryStream;
        }


        /// <summary>
        /// 压缩指定目录中的所有文件，并返回 ZIP 文件流。
        /// </summary>
        /// <param name="directoryPath">要压缩的目录路径</param>
        /// <param name="targetDirectory">保存 ZIP 文件的目标目录</param>
        /// <param name="deleteOriginals">是否删除原文件，默认值为 true</param>
        /// <returns>包含压缩内容的文件流</returns>
        public static MemoryStream ZipDirectory(string directoryPath, string targetDirectory, bool deleteOriginals = true)
        {
            // 验证目录是否存在
            if (!Directory.Exists(directoryPath))
            {
                throw new DirectoryNotFoundException($"Directory not found: {directoryPath}");
            }

            string zipFileName = $"{Path.GetFileName(directoryPath)}.zip";
            string zipFilePath = Path.Combine(targetDirectory, zipFileName);

            // 创建一个内存流用于压缩文件
            var memoryStream = new MemoryStream();

            // 创建 ZIP 文件
            using (var zipArchive = new ZipArchive(memoryStream, ZipArchiveMode.Create, leaveOpen: true))
            {
                // 获取目录中的所有文件和子目录
                foreach (var file in Directory.GetFiles(directoryPath, "*.*", SearchOption.AllDirectories))
                {
                    // 计算相对路径，以保持目录结构
                    var relativePath = Path.GetRelativePath(directoryPath, file);
                    zipArchive.CreateEntryFromFile(file, relativePath);
                }
            }

            // 将内存流重置为开始位置
            memoryStream.Position = 0;

            //// 保存 ZIP 文件到指定目录
            //using (var fileStream = new FileStream(zipFilePath, FileMode.Create, FileAccess.Write))
            //{
            //    memoryStream.CopyTo(fileStream);
            //}

            // 删除原目录
            if (deleteOriginals)
            {
                Directory.Delete(directoryPath, true); // true 表示递归删除子目录和文件
            }

            // 返回内存流
            return memoryStream;
        }


    }
}
